import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

/// FileName slide_page_route
///
/// @Author lax
/// @Date 2023/12/13
///
/// @Description

class SlidePageRoute<T> extends MaterialPageRoute<T> {
  /// 是否可以右滑返回 默认true
  final bool slidePop;

  final AxisDirection? sliderDirection;

  @override
  bool get hasScopedWillPopCallback {
    return !slidePop;
  }

  SlidePageRoute({
    required WidgetBuilder builder,
    RouteSettings? settings,
    this.slidePop = true,
    this.sliderDirection = AxisDirection.right,
  }) : super(builder: builder, settings: settings);

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation, Widget child) {
    if (sliderDirection == AxisDirection.right && (kIsWeb || Platform.isIOS)) {
      return super.buildTransitions(context, animation, secondaryAnimation, child);
    }
    return SlideTransition(
      position: Tween<Offset>(
        begin: {
          AxisDirection.left: const Offset(-1, 0),
          AxisDirection.right: const Offset(1, 0),
          AxisDirection.up: const Offset(0, -1),
          AxisDirection.down: const Offset(0, 1),
        }[sliderDirection],
        end: Offset.zero,
      ).animate(animation),
      child: child,
    );
  }
}
